import os
import hashlib
from typing import List
from fastapi import UploadFile, HTTPException
from config.settings import settings

def validate_image_file(file: UploadFile) -> bool:
    """Validate uploaded image file"""
    
    # Check file extension
    if file.filename:
        file_ext = file.filename.split('.')[-1].lower()
        if file_ext not in settings.ALLOWED_EXTENSIONS:
            raise HTTPException(
                status_code=400, 
                detail=f"File extension '{file_ext}' not allowed. Allowed extensions: {settings.ALLOWED_EXTENSIONS}"
            )
    
    # Check content type
    if not file.content_type or not file.content_type.startswith('image/'):
        raise HTTPException(status_code=400, detail="File must be an image")
    
    return True

def get_file_hash(file_data: bytes) -> str:
    """Generate MD5 hash for file data"""
    return hashlib.md5(file_data).hexdigest()

def ensure_directory_exists(directory_path: str) -> None:
    """Ensure directory exists, create if not"""
    if not os.path.exists(directory_path):
        os.makedirs(directory_path, exist_ok=True)